<?php
/**
 * Created by PhpStorm.
 * User: syzhou
 * Date: 2020-11-12
 * Time: 14:07
 */

require_once __DIR__ . '/db.php';
require_once __DIR__ . '/file.php';

$config = null;

/**
 * @var PDO
 */
$srcPDO = connectDb('srcDb');

/**
 * @var PDO
 */
$dstPDO = connectDb('dstDb');

$uploadedFilePathsSavePath = __DIR__ . '/output/uploaded_file_paths.txt';
$distinctUploadedFileSaveDirSavePath = __DIR__ . '/output/distinct_uploaded_file_save_dir.txt';

/**
 * 获取所有待迁移源项目的项目ID
 */
function getSrcProjectIds()
{
    global $srcPDO;
    $stmt = $srcPDO->query("select id from applications where project_name in ('张迅', '于胜利', '杨树鹏', '杨如刚', '索智', '陆旭东', '王新泉', '余昌平') and award_id = 2 and status_id = 'C1' order by id", PDO::FETCH_OBJ);
    $rows = $stmt->fetchAll();
    $stmt->closeCursor();
    $projectIds = array_map(function ($row) {
        return $row->id;
    }, $rows);
    return $projectIds;
}

function connectDb($connectionName)
{
    $config = getConfig();
    $pdo = getConnection($config[$connectionName], [
        PDO::ATTR_TIMEOUT => 15,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_AUTOCOMMIT => 1
    ]);
    return $pdo;
}

function getConfig()
{
    global $config;
    if ($config === null) {
        $configPath = __DIR__ . '/config.json';
        $config = json_decode_from_file($configPath, true);
    }
    return $config;
}